<?php
namespace Home\Controller;
use Think\Controller;
class ProcessController extends Controller{
	public function feeDeduction(){
		/* 基本设置 */
		$feeTimeRule = 1*60*60*24;//24小时
		$nowTime = time();

		/* 获取扣费设置 */
		$sysconfig = C('sysconfig');
		$condition['sc_id'] = $sysconfig['payment'];
		$configData = M("SystemConfig")->where($condition)->find();
		$feeData = array(
			'ordinary'=>0,
			'vip'=>0,
		);
		if(!empty($configData)){
			$feeData['ordinary'] = (int)$configData['sc_value'];
			$feeData['vip'] = (int)$configData['sc_value_2'];
		}
		$fee_moneny = 0;
		/* 获取订单数据 */
		$orderList = M('Order')->where(array('or_status'=>array('neq',2)))->select();
		foreach ($orderList as $o_k => $o_v) {
			$now_fee_num = (int)$o_v['or_deduction_nums'];
			$should_fee_num = ($nowTime-$o_v['or_create_time'])/$feeTimeRule;
			if($should_fee_num>$now_fee_num){//扣费
				$user = M('User')->where(array('us_id'=>$o_v['or_user_id']))->find();
				if(!empty($user)){
					if($o_v['or_vip']==0){//普通设备
						$fee_moneny = $feeData['ordinary'];
						if($fee_moneny>$user['us_award_money']){
							if($o_v['or_device_id']!=null){
								M("Device")->where(array('dev_id'=>$o_v['or_device_id']))->save(array('dev_status'=>1));
							}
							M("Order")->where(array('or_id'=>$o_v['or_id']))->save(array('or_game_status'=>3,'or_status'=>2,'or_end_time'=>time(),'or_end_msg'=>'余额不足','or_password'=>'******'));
						}else{
							M("Order")->where(array('or_id'=>$o_v['or_id']))->save(array('or_deduction_nums'=>($now_fee_num+1),'or_deduction_value'=>((int)$o_v['or_deduction_value']+$fee_moneny)));
							M('User')->where(array('us_id'=>$o_v['or_user_id']))->save(array('us_award_money'=>((int)$user['us_award_money']-$fee_moneny)));
						}
					}else{//VIP设备
						$fee_moneny = $feeData['vip'];
						if($fee_moneny>$user['us_cloud_money']){
							if($o_v['or_device_id']!=null){
								M("Device")->where(array('dev_id'=>$o_v['or_device_id']))->save(array('dev_status'=>1));
							}
							M("Order")->where(array('or_id'=>$o_v['or_id']))->save(array('or_game_status'=>3,'or_status'=>2,'or_end_time'=>time(),'or_end_msg'=>'余额不足','or_password'=>'******'));
						}else{
							M("Order")->where(array('or_id'=>$o_v['or_id']))->save(array('or_deduction_nums'=>($now_fee_num+1),'or_deduction_value'=>((int)$o_v['or_deduction_value']+$fee_moneny)));
							M('User')->where(array('us_id'=>$o_v['or_user_id']))->save(array('us_cloud_money'=>((int)$user['us_cloud_money']-$fee_moneny)));
						}
					}
				}
			}
		}
		exit(1);
	}
}
?>